package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.req.product.*;
import com.dd.cloud.user.res.doctor.DoctorEquityDetailRes;
import com.dd.cloud.user.res.order.PayOrderResponse;
import com.dd.cloud.user.res.product.*;
import com.dd.cloud.user.service.doctor.IDoctorEquityService;
import com.dd.cloud.user.service.product.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Lx
 * @version 1.0
 * @date 2024/1/23
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductBuyRecordService productBuyRecordService;
    @Autowired
    private IProductGiveRuleService productGiveRuleService;
    @Autowired
    private IProductGiveRecordService productGiveRecordService;
    @Autowired
    private IProductUseRecordService productUseRecordService;
    @Autowired
    private IDoctorEquityService doctorEquityService;

    /**
     * 查询收费服务产品(会员付费设置模块)
     *
     * @param req
     * @return
     */
    @GetMapping("/system/page")
    public ResponseEntity<IPage<ProductSystemPageRes>> queryProductSystemPage(@Valid ProductSystemPageReq req) {
        IPage<ProductSystemPageRes> res = productService.queryProductSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 批量修改收费服务产品
     *
     * @param reqs
     */
    @PostMapping("/system/batch/update")
    public ResponseEntity<Void> updateBatchProductSystem(@Valid @RequestBody List<ProductSystemUpdateReq> reqs) {
        productService.updateBatchProductSystem(reqs);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询收费服务产品赠送规则
     *
     * @param req
     * @return
     */
    @GetMapping("/give/rule/system/page")
    public ResponseEntity<IPage<ProductGiveRuleSystemPageRes>> queryProductGiveRuleSystemPage(@Valid ProductGiveRuleSystemPageReq req) {
        IPage<ProductGiveRuleSystemPageRes> res = productGiveRuleService.queryProductGiveRuleSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 批量修改收费服务产品赠送规则
     *
     * @param reqs
     */
    @PostMapping("/give/rule/system/batch/update")
    public ResponseEntity<Void> updateBatchProductGiveRuleSystem(@Valid @RequestBody List<ProductGiveRuleSystemUpdateReq> reqs) {
        productGiveRuleService.updateBatchProductGiveRuleSystem(reqs);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询收费服务产品赠送记录
     *
     * @param req
     * @return
     */
    @GetMapping("/give/record/system/page")
    public ResponseEntity<IPage<ProductGiveRecordSystemPageRes>> queryProductGiveRecordSystemPage(@Valid ProductGiveRecordSystemPageReq req) {
        IPage<ProductGiveRecordSystemPageRes> res = productGiveRecordService.queryProductGiveRecordSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询收费服务产品购买记录
     *
     * @param req
     * @return
     */
    @GetMapping("/buy/record/system/page")
    public ResponseEntity<IPage<ProductBuyRecordSystemPageRes>> queryProductBuyRecordSystemPage(@Valid ProductBuyRecordSystemPageReq req) {
        IPage<ProductBuyRecordSystemPageRes> res = productBuyRecordService.queryProductBuyRecordSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询收费服务产品购买记录详情
     *
     * @param id
     * @return
     */
    @GetMapping("/buy/record/system/detail")
    public ResponseEntity<ProductBuyRecordSystemDetailRes> queryProductBuyRecordSystemDetail(Integer id) {
        ProductBuyRecordSystemDetailRes res = productBuyRecordService.queryProductBuyRecordSystemDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 收费服务产品使用记录
     *
     * @param req
     * @return
     */
    @GetMapping("/use/record/system")
    public ResponseEntity<IPage<ProductUseRecordSystemPageRes>> queryProductUseRecordSystemPage(@Valid ProductUseRecordSystemPageReq req) {
        IPage<ProductUseRecordSystemPageRes> res = productUseRecordService.queryProductUseRecordSystemPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 医生查询收费产品使用记录
     *
     * @param req
     * @return
     */
    @GetMapping("/use/record/doctor")
    public ResponseEntity<IPage<ProductUseRecordDoctorPageRes>> queryProductUseRecordDoctorPage(@Valid ProductUseRecordDoctorPageReq req) {
        IPage<ProductUseRecordDoctorPageRes> res = productUseRecordService.queryProductUseRecordDoctorPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询医生账号权益
     *
     * @param doctorId 医生id
     * @return
     */
    @GetMapping("/doctor/equity")
    public ResponseEntity<DoctorEquityDetailRes> queryDoctorEquityDetail(Integer doctorId) {
        DoctorEquityDetailRes res;
        if (doctorId == null) {
            res = doctorEquityService.queryDoctorEquityDetail();
        } else {
            res = doctorEquityService.queryDoctorEquityDetail(doctorId);
        }
        return ResponseEntity.ok(res);
    }

    /**
     * 医生用户查询可购买产品服务列表
     *
     * @return
     */
    @GetMapping("/doctor/list")
    public ResponseEntity<List<ProductDoctorListRes>> queryProductDoctorList() {
        List<ProductDoctorListRes> res = productService.queryProductDoctorList();
        return ResponseEntity.ok(res);
    }

    /**
     * 购买产品服务
     *
     * @param request
     * @param req
     * @return
     */
    @PostMapping("/doctor/buy")
    public ResponseEntity<PayOrderResponse<?>> productPay(HttpServletRequest request, ProductPayReq req) {
        PayOrderResponse res = productService.productPay(request, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询服务产品购买记录弹幕
     *
     * @param req
     * @return
     */
    @GetMapping("/doctor/buy/record/barrage/page")
    public ResponseEntity<IPage<ProductBuyRecordBarragePageRes>> queryProductBuyRecordBarragePage(PageReq req) {
        IPage<ProductBuyRecordBarragePageRes> res = productBuyRecordService.queryProductBuyRecordBarragePage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 医生查询收费服务产品购买记录
     *
     * @param req
     * @return
     */
    @GetMapping("/doctor/buy/record/page")
    public ResponseEntity<IPage<ProductBuyRecordDoctorPageRes>> queryProductBuyRecordDoctorPage(ProductBuyRecordDoctorPageReq req) {
        IPage<ProductBuyRecordDoctorPageRes> res = productBuyRecordService.queryProductBuyRecordDoctorPage(req);
        return ResponseEntity.ok(res);
    }
}
